// index.js
const app=getApp()
const req=require('../../utils/req')
const util=require('../../utils/util')
import { areaList } from '../../miniprogram_npm/@vant/area-data/data';
Page({
  data: {
    motto: 'Hello World',
    userInfo: {
      avatarUrl: '',
      nickName: '',
    },
    hasUserInfo: false,
    canIUseGetUserProfile: wx.canIUse('getUserProfile'),
    canIUseNicknameComp: wx.canIUse('input.type.nickname'),
    height: app.globalData.navHeight,
    topHeight: app.globalData.height,
    navContentHeight:app.globalData.navContentHeight,
    navContentWidth:app.globalData.navContentWidth,
    today:'', //当天日期
    iconItem:[], //功能模块
    banner:[], //banner
    current:0,//banner游标
    newProList:[],//今日上新
    boomProduct:[],//爆品排行榜
    lists:[],//首页数据
    type:3, // 1:爆品推荐；2：理我最近；3：无须预约
    page:1,//页码
    limit:10,//每页显示数量
    empty:false,//是否为空
    isLoadingAll:false,//是否最后一页
    areaList,
    showArea:false
  },
  onLoad:function(options){
    this.getToDay()
    this.getBanner()
    this.getIconItem()
    this.getNewProList()
    this.getBoomProduct()
    this.getList()

    this.locationData()
    
  },
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  getToDay:function(){
    var date = new Date();
    let currentMonths=date.getMonth()+1;
    let currentDay=date.getDate();
    this.setData({
      today:currentMonths+'/'+currentDay
    })
  },
  onPageScroll: function (e) {
    var height=this.data.height
    if (e.scrollTop > 10) {
      this.setData({ 
        navbg:true
      })
    } else {
      this.setData({ 
        navbg:false
      })
    }
  }, 
  swiperChange:function(e){
    //console.log(e)
    this.setData({
      current:e.detail.current
    })
  },
  toLink:function(e){
    var url=e.currentTarget.dataset.url
    if(url){
        if(url.indexOf('http')>-1 || url.indexOf('https')>-1){
            wx.navigateTo({
              url: '/pages/webview/index?url='+url,
            })
        }else{
          wx.navigateTo({
            url: url,
          })
        }
    }else{

    }
  },
  toDetail:function(e){
    console.log(e)
    var id=e.currentTarget.dataset.id
    var path=e.currentTarget.dataset.path
    var item=e.currentTarget.dataset.item
    var url=''
    if(path=='/pages/timeCar/index'){
      url=path+'?data='+item.id+'&storeId='+item.storeId+'&storeName='+item.storeName
    }else{
      url=path+'?data='+id
    }
   
   if(path){
    wx.navigateTo({
      url: url,
    })
   }else{
     wx.showToast({
       title: '敬请期待',
       icon:'none',
       duration:2000
     })
   }
    
  },
  //功能区
  getIconItem:function(){
    var that=this
    req.request('/ht/user/homeType',{},'POST').then(res=>{
      that.setData({
        iconItem:res.data
      })
    })
  },
  toCategory:function(e){
    var id=e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/category/index/index?id='+id,
    })
  },

  //首页banner
  getBanner:function(){
    var that=this
    var data={}
    data.type=1
    req.request('/ht/user/banner',data,'POST').then(res=>{
      that.setData({
        banner:res.data
      })
    })
  },
  //今日上新
 
  getNewProList:function(){
    var that=this
    var data={}
    
    req.request('/ht/user/newProList',data,'POST').then(res=>{
      that.setData({
        newProList:res.data
      })
    })
  },

  //爆品排行榜
  
  getBoomProduct:function(){
    var that=this
    var data={}


    req.request('/ht/user/boomProduct',data,'POST').then(res=>{
      that.setData({
        boomProduct:res.data
      })
    })
  },
  //切换tab
  tab:function(e){
      var type=e.currentTarget.dataset.type
      this.setData({
        type:type,
        lists:[],
        page:1,//页码
        limit:10,//每页显示数量
        empty:false,//是否为空
        isLoadingAll:false,//是否最后一页
      })
      this.getList()
  },
  //首页数据
  getList:function(){
    var that=this
    var data={}

    data.type=this.data.type
    data.page=this.data.page
    data.limit=this.data.limit
    app.getList('/ht/user/homeProduct',data,'POST',that)

  },
 
  //获取位置坐标
  locationData() {
   
    let locationInfo = wx.getStorageSync('locationInfo')
    if(locationInfo){
      this.setData({
        latitude: locationInfo.location.lat,
        longitude: locationInfo.location.lng,
        placeName: locationInfo.address_component.city,
      })
      console.log(this.data.placeName)
    }
    else{

        util.locationData().then(res=>{
            console.log(res)
           
            this.clearGet()
            this.locationData()
        })
   
   
    }
    
  },
  // 确定选择
  bindConfirm(e){
    console.log(e)
    let value = e.detail.values
    console.log(value)
    this.setData({
      showArea:false,
      placeName:value[1].name
    })
    let address = e.detail.values[0].name + ',' + e.detail.values[1].name + ',' + e.detail.values[2].name
    let locationInfo = wx.getStorageSync('locationInfo')
    
    locationInfo.address_component.province = value[0].name
    locationInfo.address_component.city = value[1].name
    locationInfo.address_component.district = value[2].name
    wx.setStorageSync('locationInfo', locationInfo)
    this.clearGet()
  },

  //清除再获取爆款和套餐
  clearGet:function(){
    this.setData({
      lists:[],
      page:1,//页码
      limit:10,//每页显示数量
      empty:false,//是否为空
      isLoadingAll:false,//是否最后一页
      boomProduct:[]
    })
    this.getBoomProduct()
    this.getList()
  },

  // 取消选择
  bindCancel(e){
    console.log(e)
    this.setData({
      showArea:false
    })
  },
  //打开地址选择
  openSelect(){
    console.log(1)
    this.setData({
      showArea:true
    })
  },
  toFlash:function(){
    wx.showToast({
      title: '秒杀未开始，无法进入',
      icon:'none',
      duration:1500,
      mask:true
    })
  }
})
